package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.turing.usdtpay.R;
import com.turing.usdtpay.mvp.contract.RegisterContract;
import com.turing.usdtpay.mvp.presenter.RegisterPresenter;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.ToastUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/9 14:02
 * @描述：
 **/
public class RegisterActivity extends Activity implements View.OnClickListener ,RegisterContract.View {
    private TextView mLogin,mAgreement,mPrivacy,mSend;
    private EditText mAccount,mPwd,mConfirmPwd,mVerify,mInvite;
    private Button mRegister;
    private ImageView mCheck;
    private boolean isCheck;
    private RegisterContract.Present mPresent;
    private int time = 120;
    private boolean isCount = false;
    private CountDownTimer countDownTimer = new CountDownTimer(120000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            isCount = true;
            time = -- time ;
            mSend.setText(time+"秒");
        }

        @Override
        public void onFinish() {
            time = 120 ;
            isCount = false;
            mSend.setText("发送");
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        setPresenter(new RegisterPresenter(this));
    }

    private void initView() {
        mLogin = (TextView) findViewById(R.id.tv_login);
        mAgreement = (TextView)findViewById(R.id.tv_agreement);
        mPrivacy = (TextView) findViewById(R.id.tv_privacy);
        mAccount = (EditText) findViewById(R.id.et_account);
        mPwd = (EditText) findViewById(R.id.et_pwd);
        mConfirmPwd = (EditText) findViewById(R.id.et_confirm_pwd);
        mVerify = (EditText) findViewById(R.id.et_verify);
        mInvite = (EditText) findViewById(R.id.et_invite_code);
        mSend = (TextView) findViewById(R.id.tv_verify);
        mRegister = (Button) findViewById(R.id.btn_register);
        mCheck = (ImageView) findViewById(R.id.iv_check);
        mLogin.setOnClickListener(this);
        mSend.setOnClickListener(this);
        mAgreement.setOnClickListener(this);
        mPrivacy.setOnClickListener(this);
        mRegister.setOnClickListener(this);
        mCheck.setOnClickListener(this);
        isCheck = false;
        mCheck.setImageResource(R.mipmap.icon_uncheck);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == mLogin.getId()){
            finish();
        }else if (v.getId() == mSend.getId()){
            if (!FormatUtils.isEmail(mAccount.getText().toString())){
                ToastUtils.showToast(this,"请输入有效邮箱地址");
                return;
            }
            if (isCount){
                ToastUtils.showToast(this,"请稍后再试");
                return;
            }
            countDownTimer.start();
            mPresent.verify(mAccount.getText().toString(),1);
        }else if (v.getId() == mAgreement.getId()){
            startActivity(new Intent(this,AgreementActivity.class));
        }else if (v.getId() == mPrivacy.getId()){
            startActivity(new Intent(this,PrivacyActivity.class));
        }else if (v.getId() == mRegister.getId()){
            boolean b = canRegister();
            if (b){
                mPresent.register(mAccount.getText().toString(),mPwd.getText().toString(),mVerify.getText().toString(),mInvite.getText().toString());
            }
        }else if (v.getId() == mCheck.getId()){
            if (!isCheck){
                mCheck.setImageResource(R.mipmap.icon_check);
                isCheck = true;
            }else {
                mCheck.setImageResource(R.mipmap.icon_uncheck);
                isCheck = false;
            }
        }
    }

    private boolean canRegister() {
        if (!FormatUtils.isEmail(mAccount.getText().toString())){
            ToastUtils.showToast(this,"请输入有效邮箱地址");
            return false;
        }

        if (!FormatUtils.checkPasswordRule(mPwd.getText().toString())||!FormatUtils.checkPasswordRule(mConfirmPwd.getText().toString())){
            ToastUtils.showNormolToast(this,"密码格式为6-16位字符，需同时包含大小写字母和数字，可包含特殊字符");
            return false;
        }

        if (!mPwd.getText().toString().equals(mConfirmPwd.getText().toString())){
            ToastUtils.showToast(this,"两个密码不一致");
            return false;
        }
        if (!isCheck){
            ToastUtils.showToast(this,"请同意协议和隐私政策");
            return false;
        }
        if (TextUtils.isEmpty(mInvite.getText().toString())){
            ToastUtils.showToast(this,"请填写邀请码");
            return false;
        }

        return true;
    }

    @Override
    public void registerSuccess(String account,String pwd) {
        ToastUtils.showToast(this,"注册成功");
        Intent intent = new Intent();
        intent.putExtra("account",account);
        intent.putExtra("pwd",pwd);
        setResult(3,intent);
        finish();
    }

    @Override
    public void verifySuccess() {
        ToastUtils.showToast(this,"发送成功");
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showToast(this,msg);
    }

    @Override
    public void setPresenter(RegisterContract.Present presenter) {
        mPresent = presenter;
    }

    @Override
    protected void onDestroy() {
        if (countDownTimer != null){
            countDownTimer.cancel();
            countDownTimer = null;
        }
        super.onDestroy();
    }
}
